﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;

namespace Zadatak4.Tests
{
    [TestClass]
    public class BrowserTest
    {
        [TestMethod]
        public void Test_google_algebra_hr()
        {
            // Initialize the Chrome Driver
            using (var driver = new FirefoxDriver())
            {
                // otvori google.hr
                driver.Navigate().GoToUrl("https://www.google.hr");

                // u input box upisi 'algebra'
                var inputBox = driver.FindElement(By.Name("q"));
                inputBox.SendKeys("algebra");

                // klikni gumb 'search'
                driver.FindElement(By.Name("btnG")).Click();

                // cekaj max 5 sekundi da se na stranici pojavi link kojem je dio teksta 'Otvoreno učilište Algebra'
                new WebDriverWait(driver, TimeSpan.FromSeconds(5))
                    .Until(ExpectedConditions.ElementExists(By.PartialLinkText("Otvoreno učilište Algebra")));
            }
        }

        [TestMethod]
        public void Test_student_racunarstvo_hr_krivi_login()
        {
            using (var driver = new FirefoxDriver())
            {
                driver.Navigate().GoToUrl("https://student.racunarstvo.hr");
                
                IWebElement username = driver.FindElement(By.Id("username"));
                username.SendKeys("krivi user");
                
                IWebElement password = driver.FindElement(By.Id("pass"));
                password.SendKeys("krivi password");
                
                driver.FindElement(By.Name("btn_login")).Click();


                new WebDriverWait(driver, TimeSpan.FromSeconds(5))
                    .Until(PostojiPorukaDaKorisnickoImeIliLozinkaNijeTocna);
            }
        }

        public static bool PostojiPorukaDaKorisnickoImeIliLozinkaNijeTocna(IWebDriver driver)
        {
            return driver.PageSource.Contains("Greska 001: Korisničko ime i/ili lozinka nije točna.");
        }
    }
}
